import asyncio
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import time


async def async_job():
    print(f"{round(time.time(), 1)} 执行异步任务...")
    # 模拟异步任务
    await asyncio.sleep(0.1)


async def async_task():
    count = 1
    while True:
        print(f"{round(time.time(), 1)} 异步任务 count:{count}")
        count += 1
        await asyncio.sleep(2)


async def main():
    scheduler = AsyncIOScheduler()
    # 每2秒执行一次异步任务
    scheduler.add_job(async_job, "interval", seconds=2)
    scheduler.start()
    await asyncio.gather(async_task())


if __name__ == "__main__":
    asyncio.run(main())

""" 运行结果:
1761877815.8 异步任务 count:1
1761877817.8 异步任务 count:2
1761877817.8 执行异步任务...
1761877819.8 异步任务 count:3
1761877819.8 执行异步任务...

符合预期: 异步定时器 异步执行, 查看只有一个线程, 适合低精度定时任务
"""